 <?php
                        
	include 'conn.php';		
	
	$request_body = file_get_contents('php://input');
    $data = json_decode($request_body, true);

	if($_GET['lx']=='list'){
		$page = isset($data['page']) ? intval($data['page']) : 1;
		$rows = isset($data['size']) ? intval($data['size']) : 10;
		$offset = ($page-1)*$rows;
		
		$sort = isset($data['sort']) ? strval($data['sort']) : 'id';
		$order = isset($data['order']) ? strval($data['order']) : 'desc';
		
		$q = isset($data['nr']) ? $data['nr'] : '';
		$school = isset($data['school']) ? $data['school'] : '';

		$where = "kh like '%$q%' ";
		$sql="select * from gk_25 where ".$where." order by $sort $order limit $offset,$rows";
		
		$rows = array();
		$rs =  mysqli_query($con,$sql);
		while($row = $rs->fetch_assoc()){
			array_push($rows,$row);
		} 

		$rs = mysqli_query($con,"select count(*) as num from gk_25 where ".$where." ");
		$row = $rs->fetch_assoc();
		$result["total"] = $row["num"];

		$result["code"] = 200;
		$result["data"] = $rows;
		$result["message"] = $sql;
		echo json_encode($result);
		
	}elseif($_GET['lx']=='save'){
		
		if($_GET['addEdit']=='edit'){
			$sql="UPDATE js_9_vue_el set ";
			foreach ($data as $k => $v) {		
				$sql=$sql. $k . "='" . $v . "',";
			}
			$sql=substr($sql,0,-1);		//去除最后一个字符 ','
			
			$sql=$sql." where id=".$data['id'];		
			
			$result["message"] = "修改成功！";

		}elseif($_GET['addEdit']=='add'){

			$sql="INSERT INTO js_9_vue_el(";	
		
			foreach ($data as $k => $v) {		
				$cols=$cols.$k.",";
				$value=$value."'".$v."',";
			}
			$cols=substr($cols,0,-1);	
			$value=substr($value,0,-1);

			$sql=$sql.$cols.")VALUES(".$value.")";

			$result["message"] = "添加成功！";
		}

		mysql_query($sql);
		
		$result["code"] = 200;		
		$result['sql']=$sql;
		echo json_encode($result);
		
	}elseif($_GET['lx']=='del'){
				
		$sql="DELETE FROM js_9_vue_el WHERE id=".$data['id'];
		mysql_query($sql);
		
		$result["code"] = 200;
		$result["message"] = "删除成功！";
		$result['sql']=$sql;
		echo json_encode($result);
		
	}
    
?>